package com.lixinkeji.extension

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Rect
import android.media.MediaMetadataRetriever
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.FitCenter
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.lixinkeji.base.R
import com.lixinkeji.data.ImageInfo
import com.lixinkeji.groupavatars.GroupAvatarsLib
import com.lixinkeji.groupavatars.layout.DingLayoutManager
import com.lixinkeji.groupavatars.layout.ILayoutManager
import com.lixinkeji.groupavatars.utils.DisplayUtils
import com.lixinkeji.transformation.MosaicBitmapTransformation
import com.lixinkeji.utils.BlurTransformation
import com.lixinkeji.widget.GridImageView
import com.lixinkeji.widget.XBaseAdapter
import com.lixinkeji.activity.MyPreviewActivity
import com.previewlibrary.GPreviewBuilder


/**
 *
 * @ClassName ImageViewExtension
 * @Author lwc
 * @Email 1679114787@qq.com
 * @Date 2021/12/24 17:10
 *
 */
/**
 * 加载图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun ImageView.load(url: String, resourceId: Int = R.drawable.mx_common_divider) {
    var options = RequestOptions()
    options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
        .frame(0)
        .centerCrop()
    Glide.with(this.context).setDefaultRequestOptions(options).load(url).placeholder(this.drawable)
        .error(resourceId).into(this)

}

/**
 * 加载圆形图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun ImageView.loadCircle(url: String, resourceId: Int = R.drawable.mx_common_divider) {
    var options = RequestOptions()
    options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
        .frame(0)
        .centerCrop()
    Glide.with(this.context).setDefaultRequestOptions(options).load(url).error(resourceId)
        .circleCrop()
        .into(this)
}

/**
 * 加载圆形图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun ImageView.loadCircleResource(url: String, resourceId: Int = R.drawable.mx_common_divider) {
    var options = RequestOptions()
    options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
        .frame(0)
        .centerCrop()
    Glide.with(this.context).setDefaultRequestOptions(options).load(url).error(resourceId)
        .circleCrop()
        .into(this)
}


/**
 * 加载圆形图片  防止闪烁
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun ImageView.loadCircle2(
    iv: ImageView,
    url: String,
    resourceId: Int = R.drawable.mx_common_divider
) {
    var options = RequestOptions()
    options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
        .frame(0)
        .centerCrop()
    Glide.with(this.context).setDefaultRequestOptions(options).load(url).placeholder(iv.drawable)
        .error(resourceId).circleCrop()
        .into(this)
}


/**+
 * 加载圆角图
 * @receiver ImageView
 * @param url String
 * @param radius Int
 * @param resourceId Int
 */
fun ImageView.loadRound(
    url: String,
    radius: Int = 10,
    centerCrop: Boolean = false,
    resourceId: Int = R.drawable.mx_common_divider
) {
    var options = RequestOptions()
    options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
        .frame(0)
        .centerCrop()
    Glide.with(this.context).setDefaultRequestOptions(options).load(url).apply(
        RequestOptions().error(resourceId).transform(
            MultiTransformation(
                if (centerCrop) CenterCrop() else FitCenter(),
                RoundedCorners(radius.dp)
            )
        )
    ).into(this)
}

/**+
 * 加载固定尺寸圆角图
 * @receiver ImageView
 * @param url String
 * @param radius Int
 * @param resourceId Int
 */
fun ImageView.loadRoundSetSize(
    url: String,
    radius: Double = 7.5,
    centerCrop: Boolean = false,
    resourceId: Int = R.drawable.mx_common_divider
) {
    var options = RequestOptions()
    options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
        .frame(0)
        .centerCrop()
    Glide.with(this.context).setDefaultRequestOptions(options).load(url)
        .override(DisplayUtils.dp2px(context, 60), DisplayUtils.dp2px(context, 60)).apply(
            RequestOptions().error(resourceId).transform(
                MultiTransformation(
                    if (centerCrop) CenterCrop() else FitCenter(),
                    RoundedCorners(radius.dp)
                )
            )
        ).into(this)


}

/**+
 * 加载固定尺寸圆角图
 * @receiver ImageView
 * @param url String
 * @param radius Int
 * @param resourceId Int
 */
fun ImageView.loadVideoRoundSetSize(
    url: String,
    radius: Double = 7.5,
    centerCrop: Boolean = false,
    resourceId: Int = R.drawable.mx_common_divider
) {
    var options = RequestOptions()
    options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
        .frame(0)
        .centerCrop()

    Glide.with(this.context).setDefaultRequestOptions(options).load(url)
        .override(DisplayUtils.dp2px(context, 60), DisplayUtils.dp2px(context, 60)).apply(
            RequestOptions().error(resourceId).transform(
                MultiTransformation(
                    if (centerCrop) CenterCrop() else FitCenter(),
                    RoundedCorners(radius.dp)
                )
            )
        ).into(this)

}


fun ImageView.loadRoundWith(
    url: String,
    radius: Int = 10,
    centerCrop: Boolean = false,
    resourceId: Int = R.drawable.place_picture,
    width: Int? = null,
    height: Int? = null
) {
    var options = RequestOptions()
    options.skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.ALL)
        .frame(0)
        .centerCrop();

    if (height != null && width != null) {
        if (height == 0 || width == 0) {
            Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url).error(resourceId)
                .apply(
                    RequestOptions().transform(
                        MultiTransformation(
                            if (centerCrop) CenterCrop() else FitCenter(),
                            RoundedCorners(radius.dp)
                        )
                    )
                ).dontAnimate().into(this)
        } else {
            if (width < height) {
                if (width * 1.8 < height) {
                    Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url)
                        .error(resourceId).override(width, (1.8 * width).toInt()).apply(
                        RequestOptions().transform(
                            MultiTransformation(
                                if (centerCrop) CenterCrop() else FitCenter(),
                                RoundedCorners(radius.dp)
                            )
                        )
                    ).dontAnimate().into(this)
                } else {
                    Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url)
                        .error(resourceId).override(width, height).apply(
                        RequestOptions().transform(
                            MultiTransformation(
                                if (centerCrop) CenterCrop() else FitCenter(),
                                RoundedCorners(radius.dp)
                            )
                        )
                    ).dontAnimate().into(this)
                }

            } else if (width > height) {
                Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url)
                    .error(resourceId).override(width, height).apply(
                    RequestOptions().transform(
                        MultiTransformation(
                            if (centerCrop) CenterCrop() else FitCenter(),
                            RoundedCorners(radius.dp)
                        )
                    )
                ).dontAnimate().into(this)
            } else {
                Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url)
                    .error(resourceId).override(width, height).apply(
                    RequestOptions().transform(
                        MultiTransformation(
                            if (centerCrop) CenterCrop() else FitCenter(),
                            RoundedCorners(radius.dp)
                        )
                    )
                ).dontAnimate().into(this)
            }
        }
    } else {
        Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url).error(resourceId)
            .apply(
                RequestOptions().transform(
                    MultiTransformation(
                        if (centerCrop) CenterCrop() else FitCenter(),
                        RoundedCorners(radius.dp)
                    )
                )
            ).dontAnimate().into(this)

    }
}



//fun ImageView.loadRoundWithPreload(
//    url: String,
//    radius: Int = 10,
//    centerCrop: Boolean = false,
//    resourceId: Int = R.drawable.place_picture,
//    width: Int? = null,
//    height: Int? = null
//) {
//    var options = RequestOptions()
//    options.skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.ALL)
//        .frame(0)
//        .centerCrop();
//
//    if (height != null && width != null) {
//        if (height == 0 || width == 0) {
//            Glide.with(this.context).setDefaultRequestOptions(options).load(url).error(resourceId)
//                .apply(
//                    RequestOptions().transform(
//                        MultiTransformation(
//                            if (centerCrop) CenterCrop() else FitCenter(),
//                            RoundedCorners(radius.dp)
//                        )
//                    )
//                ).preload(width, height)
//        } else {
//            if (width < height) {
//                if (width * 1.8 < height) {
//                    Glide.with(this.context).setDefaultRequestOptions(options).load(url)
//                        .error(resourceId).override(width, (1.8 * width).toInt()).apply(
//                            RequestOptions().transform(
//                                MultiTransformation(
//                                    if (centerCrop) CenterCrop() else FitCenter(),
//                                    RoundedCorners(radius.dp)
//                                )
//                            )
//                        ).preload(width, height)
//                } else {
//                    Glide.with(this.context).setDefaultRequestOptions(options).load(url)
//                        .error(resourceId).override(width, height).apply(
//                            RequestOptions().transform(
//                                MultiTransformation(
//                                    if (centerCrop) CenterCrop() else FitCenter(),
//                                    RoundedCorners(radius.dp)
//                                )
//                            )
//                        ).preload(width, height)
//                }
//
//            } else if (width > height) {
//                Glide.with(this.context).setDefaultRequestOptions(options).load(url)
//                    .error(resourceId).override(width, height).apply(
//                        RequestOptions().transform(
//                            MultiTransformation(
//                                if (centerCrop) CenterCrop() else FitCenter(),
//                                RoundedCorners(radius.dp)
//                            )
//                        )
//                    ).preload(width, height)
//            } else {
//                Glide.with(this.context).setDefaultRequestOptions(options).load(url)
//                    .error(resourceId).override(width, height).apply(
//                        RequestOptions().transform(
//                            MultiTransformation(
//                                if (centerCrop) CenterCrop() else FitCenter(),
//                                RoundedCorners(radius.dp)
//                            )
//                        )
//                    ).preload(width, height)
//            }
//        }
//    } else {
//        Glide.with(this.context).setDefaultRequestOptions(options).load(url).error(resourceId)
//            .apply(
//                RequestOptions().transform(
//                    MultiTransformation(
//                        if (centerCrop) CenterCrop() else FitCenter(),
//                        RoundedCorners(radius.dp)
//                    )
//                )
//            ).preload()
//
//    }
//}




@SuppressLint("CheckResult")
fun ImageView.loadVideoRoundWith(
    url: String,
    radius: Int = 10,
    centerCrop: Boolean = false,
    resourceId: Int = R.drawable.default_img_head,
    width: Int? = null,
    height: Int? = null
) {
    var options = RequestOptions()
    options.skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.ALL)
        .frame(0)
        .centerCrop();

    if (height != null && width != null) {
        if (height == 0 || width == 0) {
            Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url).apply(
                RequestOptions().error(resourceId).transform(
                    MultiTransformation(
                        if (centerCrop) CenterCrop() else FitCenter(),
                        RoundedCorners(radius.dp)
                    )
                )
            ).dontAnimate().into(this)
        } else {
            if (width < height) {
                if (width * 1.8 < height) {
                    Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url)
                        .override(width, (1.8 * width).toInt()).apply(
                        RequestOptions().error(resourceId).transform(
                            MultiTransformation(
                                if (centerCrop) CenterCrop() else FitCenter(),
                                RoundedCorners(radius.dp)
                            )
                        )
                    ).dontAnimate().into(this)
                } else {
                    Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url)
                        .override(width, height).apply(
                        RequestOptions().error(resourceId).transform(
                            MultiTransformation(
                                if (centerCrop) CenterCrop() else FitCenter(),
                                RoundedCorners(radius.dp)
                            )
                        )
                    ).dontAnimate().into(this)
                }

            } else if (width > height) {
                Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url)
                    .override(width, height).apply(
                    RequestOptions().error(resourceId).transform(
                        MultiTransformation(
                            if (centerCrop) CenterCrop() else FitCenter(),
                            RoundedCorners(radius.dp)
                        )
                    )
                ).dontAnimate().into(this)
            } else {
                Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url)
                    .override(width, height).apply(
                    RequestOptions().error(resourceId).transform(
                        MultiTransformation(
                            if (centerCrop) CenterCrop() else FitCenter(),
                            RoundedCorners(radius.dp)
                        )
                    )
                ).dontAnimate().into(this)
            }
        }
    } else {
        Glide.with(this.context).setDefaultRequestOptions(options).asBitmap().load(url).apply(
            RequestOptions().error(resourceId).transform(
                MultiTransformation(
                    if (centerCrop) CenterCrop() else FitCenter(),
                    RoundedCorners(radius.dp)
                )
            )
        ).dontAnimate().into(this)
    }
}


fun ImageView.loadVideoRoundWith(
    url: String,
    radius: Int = 10,
    centerCrop: Boolean = false,
    resourceId: Int = R.drawable.default_img,
    width: Int? = null,
    height: Int? = null,
    sl_imgUrl: String? = null,
    imgUrl: String? = null
) {
    if (sl_imgUrl != null) {
        var imgUrlTrue = sl_imgUrl.substring(2, (sl_imgUrl?.length)!! - 2)
        if (imgUrlTrue.isEmpty()) {
            if (imgUrl != null) {
                imgUrlTrue = imgUrl.substring(2, (imgUrl?.length)!! - 2)
            }
        }

        if (height != null && width != null) {
            if (height == 0 || width == 0) {
                Glide.with(this.context).load(imgUrlTrue).apply(
                    RequestOptions().error(resourceId).transform(
                        MultiTransformation(
                            if (centerCrop) CenterCrop() else FitCenter(),
                            RoundedCorners(radius.dp)
                        )
                    )
                ).into(this)
            } else {
                if (width < height) {
                    if (width * 1.8 < height) {
                        Glide.with(this.context).load(imgUrlTrue)
                            .override(width, (1.8 * width).toInt()).apply(
                            RequestOptions().error(resourceId).transform(
                                MultiTransformation(
                                    if (centerCrop) CenterCrop() else FitCenter(),
                                    RoundedCorners(radius.dp)
                                )
                            )
                        ).into(this)
                    } else {
                        Glide.with(this.context).load(imgUrlTrue).override(width, height).apply(
                            RequestOptions().error(resourceId).transform(
                                MultiTransformation(
                                    if (centerCrop) CenterCrop() else FitCenter(),
                                    RoundedCorners(radius.dp)
                                )
                            )
                        ).into(this)
                    }

                } else if (width > height) {
                    Glide.with(this.context).load(imgUrlTrue).override(width, height).apply(
                        RequestOptions().error(resourceId).transform(
                            MultiTransformation(
                                if (centerCrop) CenterCrop() else FitCenter(),
                                RoundedCorners(radius.dp)
                            )
                        )
                    ).into(this)
                } else {
                    Glide.with(this.context).load(imgUrlTrue).override(width, height).apply(
                        RequestOptions().error(resourceId).transform(
                            MultiTransformation(
                                if (centerCrop) CenterCrop() else FitCenter(),
                                RoundedCorners(radius.dp)
                            )
                        )
                    ).into(this)
                }


            }


        } else {
            Glide.with(this.context).load(imgUrl).apply(
                RequestOptions().error(resourceId).transform(
                    MultiTransformation(
                        if (centerCrop) CenterCrop() else FitCenter(),
                        RoundedCorners(radius.dp)
                    )
                )
            ).into(this)

        }
    } else {
        if (height != null && width != null) {
            if (height == 0 || width == 0) {
                Glide.with(this.context).load(url).apply(
                    RequestOptions().error(resourceId).transform(
                        MultiTransformation(
                            if (centerCrop) CenterCrop() else FitCenter(),
                            RoundedCorners(radius.dp)
                        )
                    )
                ).into(this)
            } else {
                if (width < height) {
                    if (width * 1.8 < height) {
                        Glide.with(this.context).load(url).override(width, (1.8 * width).toInt())
                            .apply(
                                RequestOptions().error(resourceId).transform(
                                    MultiTransformation(
                                        if (centerCrop) CenterCrop() else FitCenter(),
                                        RoundedCorners(radius.dp)
                                    )
                                )
                            ).into(this)
                    } else {
                        Glide.with(this.context).load(url).override(width, height).apply(
                            RequestOptions().error(resourceId).transform(
                                MultiTransformation(
                                    if (centerCrop) CenterCrop() else FitCenter(),
                                    RoundedCorners(radius.dp)
                                )
                            )
                        ).into(this)
                    }

                } else if (width > height) {
                    Glide.with(this.context).load(url).override(width, height).apply(
                        RequestOptions().error(resourceId).transform(
                            MultiTransformation(
                                if (centerCrop) CenterCrop() else FitCenter(),
                                RoundedCorners(radius.dp)
                            )
                        )
                    ).into(this)
                } else {
                    Glide.with(this.context).load(url).override(width, height).apply(
                        RequestOptions().error(resourceId).transform(
                            MultiTransformation(
                                if (centerCrop) CenterCrop() else FitCenter(),
                                RoundedCorners(radius.dp)
                            )
                        )
                    ).into(this)
                }


            }


        } else {
            Glide.with(this.context).load(url).apply(
                RequestOptions().error(resourceId).transform(
                    MultiTransformation(
                        if (centerCrop) CenterCrop() else FitCenter(),
                        RoundedCorners(radius.dp)
                    )
                )
            ).into(this)

        }
    }

}


/**
 * 加载马赛克
 * @receiver ImageView
 * @param url String
 * @param rect Rect?
 * @param size Int
 * @param circleCrop Boolean
 * @param resourceId Int
 */
fun ImageView.loadMosaic(
    url: String,
    rect: Rect? = null,
    size: Int = 20,
    circleCrop: Boolean = false,
    resourceId: Int = R.drawable.mx_common_divider
) {
    Glide.with(this.context).load(url).apply(
        RequestOptions().error(resourceId).transform(
            MultiTransformation(
                MosaicBitmapTransformation(rect, size),
                if (circleCrop) CircleCrop() else CenterCrop()
            )
        )
    ).into(this)
}

/**
 * 高斯模糊
 * @receiver ImageView
 * @param url String
 * @param blurRadius Int
 * @param centerCrop Boolean
 * @param resourceId Int
 */
fun ImageView.loadBlur(
    url: String,
    blurRadius: Int = 10,
    centerCrop: Boolean = false,
    resourceId: Int = R.drawable.mx_common_divider
) {
    Glide.with(this.context).load(url).apply(
        RequestOptions().error(resourceId).transform(
            MultiTransformation(
                if (centerCrop) CenterCrop() else FitCenter(),
                BlurTransformation(blurRadius.dp)
            )
        )
    ).into(this)
}

/**
 * 大图预览
 * @receiver ImageView
 * @param position Int
 * @param urlList List<String>
 */
fun ImageView.preview(activity: Activity, position: Int = 0, urlList: List<String>) {
    val mutableList = mutableListOf<ImageInfo>()
    val bounds = Rect()
    getGlobalVisibleRect(bounds)
    urlList.forEach {
        val imgInfo = ImageInfo()
        imgInfo.url = it
        imgInfo.bounds = bounds
        mutableList.add(imgInfo)
    }
    GPreviewBuilder.from(activity) //activity实例必须
        .to(MyPreviewActivity::class.java)
//        .to(CustomActivity::class.java) //自定义Activity 使用默认的预览不需要
        .setData(mutableList) //集合
//        .setUserFragment(UserFra gment::class.java) //自定义Fragment 使用默认的预览不需要
        .setCurrentIndex(position)
        .setSingleFling(true) //是否在黑屏区域点击返回
        .isDisableDrag(false) //是否禁用图片拖拽返回
        .setType(GPreviewBuilder.IndicatorType.Dot) //指示器类型
        .setIsScale(true)
        .start() //启动

//    ImagePreview.instance // 上下文，必须是activity，不需要担心内存泄漏，本框架已经处理好
//        .setContext(this.context) // 从第几张图片
//        // 开始，索引从0开始哦~
//        .setIndex(position) //=================================================================================================
//        // 有三种设置数据集合的方式，根据自己的需求进行三选一：
//        // 1：第一步生成的imageInfo List
//        //.setImageInfoList(imageInfoList)
//        // 2：直接传url List
//        .setImageList(urlList) // 3：只有一张图片的情况，可以直接传入这张图片的url
//        //.setImage(String image)
//        //=================================================================================================
//        // 加载策略，默认为手动模式（具体可看下面加载策略的详细说明）
//        .setLoadStrategy(ImagePreview.LoadStrategy.Default) // 保存的文件夹名称，会在SD卡根目录进行文件夹的新建。
//        // (你也可设置嵌套模式，比如："BigImageView/Download"，会在SD卡根目录新建BigImageView文件夹，并在BigImageView文件夹中新建Download文件夹)
//        .setFolderName("BigImageView/Download") // 缩放动画时长，单位ms
//        .setZoomTransitionDuration(300) // 是否启用点击图片关闭。默认启用
//        .setEnableClickClose(true) // 是否启用上拉/下拉关闭。默认不启用
//        .setEnableDragClose(true) // 是否显示关闭页面按钮，在页面左下角。默认不显示
//        .setShowCloseButton(false) // 设置关闭按钮图片资源，可不填，默认为库中自带：R.drawable.ic_action_close
//        .setCloseIconResId(cc.shinichi.library.R.drawable.ic_action_close) // 是否显示下载按钮，在页面右下角。默认显示
//        .setShowDownButton(false) // 设置下载按钮图片资源，可不填，默认为库中自带：R.drawable.icon_download_new
//        .setDownIconResId(cc.shinichi.library.R.drawable.icon_download_new) // 设置是否显示顶部的指示器（1/9）默认显示
//        .setShowIndicator(true) // 设置失败时的占位图，默认为库中自带R.drawable.load_failed，设置为 0 时不显示
//        .setErrorPlaceHolder(cc.shinichi.library.R.drawable.load_failed) // 点击回调
//        .start()
}

/**
 * 群组头像
 * @receiver ImageView
 * @param groupId String
 * @param size Int
 * @param data List<String>
 * @param round Int
 * @param space Int
 * @param childAvatarRound Int
 * @param iLayoutManager ILayoutManager
 * @param textBgColor Int
 * @param bgColor Int
 * @param resourceId Int
 */
fun ImageView.groupAvatars(
    groupId: String,
    size: Int,
    data: List<String>,
    round: Int = 0,
    space: Int = 0,
    childAvatarRound: Int = 0,
    iLayoutManager: ILayoutManager = DingLayoutManager(),
    textBgColor: Int = com.ypx.imagepicker.R.color.white_F5,
    bgColor: Int = com.ypx.imagepicker.R.color.white_F5,
    resourceId: Int = R.drawable.mx_common_divider

) {
    GroupAvatarsLib.init(context)
        // 必选，设置最终生成的图片尺寸，单位dp（一般就是当前imageView的大小）
        .setSize(size)
        // 设置钉钉或者微信群头像类型 DingLayoutManager、 WechatLayoutManager
        // 目前钉钉最多组合4个，微信最多9个。超出会自动截取前4或9个
        .setLayoutManager(iLayoutManager)
        // 设置使用昵称生成头像时的背景颜色
        .setNickAvatarColor(textBgColor)
        // 设置昵称生成头像时的文字大小 ,单位dp （设置为0时 = 单个小头像的1/4大小）
        .setNickTextSize(0)
        // 设置群组ID，用于生成缓存key
        .setGroupId(groupId)
        // 设置加载最终图片的圆角大小，单位dp，默认0
        .setRound(round)
        // 设置内部单个图片的圆角，单位dp，默认0
        .setChildAvatarRound(childAvatarRound)
        // 单个图片之间的距离，单位dp，默认0dp
        .setGap(space)
        // 设置生成的图片背景色间距
        .setGapColor(bgColor)
        // 单个网络图片加载失败时，会展示默认图片
        .setPlaceholder(resourceId)
        // 设置数据（可设置网络图片地址或者昵称）
        .setDatas(data)
        // 设置要显示最终图片的ImageView
        .setImageView(this)
        .build()
}


fun GridImageView.imagesAdapter(imageList: List<String>, onClick: Boolean = true) {
    setAdapter(object : XBaseAdapter() {
        override val count: Int
            get() = imageList.size

        override fun getView(position: Int, parent: ViewGroup?): View? {

            val view = LayoutInflater.from(context)
                .inflate(R.layout.fragment_image_item, parent, false)
            val findViewById = view.findViewById<ImageView>(R.id.image)
            if (onClick) {
                findViewById.onClick {
                    if (imageList[position].isNotEmpty()) {
                        findViewById.preview(context as Activity, position, imageList)
                    }
                }
            }
            if (imageList[position].isNotEmpty()) {
                findViewById.loadRound(
                    imageList[position],
                    4,
                    count > 1
                )
            } else {
                findViewById.setImageResource(R.drawable.zw_white)
            }
            return view
        }
    })
}

fun GridImageView.imagesCommonAdapter(imageList: ArrayList<String>, onClick: Boolean = true) {
    val oldImageList = ArrayList<String>()
    oldImageList.addAll(imageList)
    if (imageList.size == 4) {
        imageList.add(2, "")
        imageList.add(5, "")
    }
    setAdapter(object : XBaseAdapter() {
        override val count: Int
            get() = imageList.size

        override fun getView(position: Int, parent: ViewGroup?): View? {

            val view = LayoutInflater.from(context)
                .inflate(R.layout.fragment_image_item, parent, false)
            val findViewById = view.findViewById<ImageView>(R.id.image)
            if (onClick) {
                findViewById.onClick {
                    if (imageList[position].isNotEmpty()) {
                        findViewById.preview(context as Activity, position, oldImageList)
                    }
                }
            }
            if (imageList[position].isNotEmpty()) {
                findViewById.loadRound(
                    imageList[position],
                    12,
                    count > 1
                )
            } else {
                findViewById.setImageResource(R.drawable.zw_white)
            }
            return view
        }
    })
}

/**
 * 加载网络视频封面
 * @receiver ImageView
 * @param url String
 */
fun ImageView.loadVideoImg(url: String) {
    val mmr = MediaMetadataRetriever()
    mmr.setDataSource(url, hashMapOf())
    val bitmap = mmr.getFrameAtTime(-1)
    var width = bitmap?.width
    var height = bitmap?.height
    if (width != null) {
        if (width > height!!) {

        }
    }


    setImageBitmap(mmr.getFrameAtTime(-1))
    mmr.release()
}